﻿using System;
using System.Collections.Generic;
using Entities;
using Data;
using System.Text.RegularExpressions;
using System.Threading;
using System.Linq;


namespace Logic
{
    public sealed class UserLogic
    {

        public Users GetUserLogic(Users i, string key)
        {
            i.rut = i.rut.CleanParameter<string>();
            i.password = i.password.CleanParameter<string>();
            if (i.rut.ValidateRut() && i.password.ValidatePassword())
            {

                UserData userData = new UserData();
                i.password = Encryption.EncriptData(i.password, key);
                Users user = userData.GetUser(i);

                if (user != null)
                {
                    List<Rol> listRoles = userData.GetListRolesById(Convert.ToInt32(user.id));
                    if (listRoles != null)
                    {

                        user.usersRoles = listRoles;
                        return user;

                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
                //return roles;
            }
            else
            {
                return null;
            }
        }

        public bool ValidateCaptcha(string userCode, string captchaCode)
        {
            userCode = userCode.CleanParameter<string>();
            if (userCode.ValidateText(5) && captchaCode != null)
            {
                if (userCode.ToLower() == captchaCode.ToLower())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }

        public bool UpdateUserByIdLogic(Users i, string key)
        {
            bool response = false;
            i.name = i.name.CleanParameter<string>();
            i.lastName = i.lastName.CleanParameter<string>();
            i.email = i.email.CleanParameter<string>();
            i.id = i.id.CleanParameter<string>();
            i.cellPhone = i.cellPhone.CleanParameter<string>();
            i.secondLastName = i.secondLastName.CleanParameter<string>();
            i.middleName = i.middleName.CleanParameter<string>();
            i.telephone = i.telephone.CleanParameter<string>();

            foreach (Rol r in i.usersRoles)
            {
                string temp = Encryption.DecryptData(r.id, key);
                r.id = temp != null ? temp.CleanParameter<string>() : "";
                r.description = r.description.CleanParameter<string>();
            }

            if (i.name.ValidateText(30) && i.lastName.ValidateText(30) && i.middleName.ValidateText(30) && i.secondLastName.ValidateText(30) &&
                    i.email.ValidateMail(50) && i.telephone.ValidateTelephone(9) && i.cellPhone.ValidateTelephone(9) && i.id.ToString().IsNumber() && i.usersRoles.ValidateRol())
            {
                response = new UserData().UpdateUserById(i);
            }
            return response;
        }

        public bool InsertUserLogic(Users i, string key, string userMail, string passwordMail, string mailServer)
        {
            bool response = false;
            i.name = i.name.CleanParameter<string>();
            i.lastName = i.lastName.CleanParameter<string>();
            i.email = i.email.CleanParameter<string>();
            i.cellPhone = i.cellPhone.CleanParameter<string>();
            i.secondLastName = i.secondLastName.CleanParameter<string>();
            i.middleName = i.middleName.CleanParameter<string>();
            i.telephone = i.telephone.CleanParameter<string>();

            foreach (Rol r in i.usersRoles)
            {
                string temp = Encryption.DecryptData(r.id, key);
                r.id = temp != null ? temp.CleanParameter<string>() : "";
                r.description = r.description.CleanParameter<string>();
            }


            if (i.name.ValidateText(30) && i.lastName.ValidateText(30) && i.middleName.ValidateText(30) && i.secondLastName.ValidateText(30) &&
               i.email.ValidateMail(50) && i.telephone.ValidateTelephone(9) && i.cellPhone.ValidateTelephone(9) && i.usersRoles.ValidateRol())
            {
                UserData userData = new UserData();
                string passDecrypt = Password.Generate(i);
                i.password = Encryption.EncriptData(passDecrypt, key);
                response = userData.InsertUser(i);

                if (response)
                {
                    Mail.SendMail(i.email, StringBuilderFunctions.JoinString(i.name, " ", i.lastName, " ", "su contraseña es:", passDecrypt), "Contraseña Usuario", userMail, passwordMail, mailServer, null);
                }

            }
            return response;
        }

        public List<Users> SearchUsersByRutLogic(Users i, string key)
        {
            i.rut = i.rut.CleanParameter<string>();
            if (i.rut.ValidateRut())
            {
                UserData userData = new UserData();
                List<Users> response = userData.SearchUsersByRut(i);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }

            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByNameLogic(string name, string key)
        {
            name = name.CleanParameter<string>();
            if (name.ValidateText(30))
            {
                List<Users> response = new UserData().SearchUsersByName(name);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByLastNameLogic(string lastName, string key)
        {
            lastName = lastName.CleanParameter<string>();
            if (lastName.ValidateText(30))
            {
                List<Users> response = new UserData().SearchUsersByLastName(lastName);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByEmailLogic(string email, string key)
        {
            email = email.CleanParameter<string>();
            if (email.ValidateMail(50))
            {
                List<Users> response = new UserData().SearchUsersByEmail(email);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByTelephoneLogic(string telephone, string key)
        {
            telephone = telephone.CleanParameter<string>();
            if (telephone.ValidateTelephone(9))
            {
                List<Users> response = new UserData().SearchUsersByTelephone(telephone);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByCellPhoneLogic(string cellphone, string key)
        {
            cellphone = cellphone.CleanParameter<string>();
            if (cellphone.ValidateTelephone(9))
            {
                List<Users> response = new UserData().SearchUsersByCellPhone(cellphone);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByDateLogic(DateTime date, string key)
        {
            date = date.CleanParameter<DateTime>();
            if (date.ValidateDate())
            {
                List<Users> response = new UserData().SearchUsersByDate(date);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByBlockedLogic(string blocked, string key)
        {
            bool response;
            blocked = blocked.CleanParameter<string>();
            if (blocked.ValidateText(2))
            {
                if (blocked.ToLower() == "si")
                {
                    response = true;
                }
                else
                {
                    response = false;
                }
                List<Users> listUser = new UserData().SearchUsersByBlocked(response);
                if (listUser != null)
                {
                    foreach (Users cc in listUser)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return listUser;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> SearchUsersByRolLogic(string rol, string key)
        {
            rol = rol.CleanParameter<string>();
            if (rol.ValidateText(50))
            {
                List<Users> response = new UserData().SearchUsersByRol(rol);
                if (response != null)
                {
                    foreach (Users cc in response)
                    {
                        cc.id = Encryption.EncriptData(cc.id.ToString(), key);

                    }

                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public List<Users> GetListUsersLogic(string key)
        {
            UserData userData = new UserData();
            List<Users> encriptList = new List<Users>();
            List<Users> dbList = userData.GetListUsers();
            if (dbList != null)
            {
                foreach (Users cc in dbList)
                {
                    cc.id = Encryption.EncriptData(cc.id.ToString(), key);
                    encriptList.Add(cc);
                }

                return encriptList;
            }
            else
            {
                return null;
            }
        }

        public List<string> AutocompleteByNameLogic(string name)
        {
            name = name.CleanParameter<string>();
            if (name.ValidateText(30))
            {
                List<string> response = new UserData().AutocompleteByName(name);
                return response;
            }
            else
            {
                return null;
            }
        }

        public List<string> AutocompleteByLastNameLogic(string lastName)
        {
            lastName = lastName.CleanParameter<string>();
            if (lastName.ValidateText(30))
            {
                List<string> response = new UserData().AutocompleteByLastName(lastName);
                return response;
            }
            else
            {
                return null;
            }
        }

        public List<string> AutocompleteByEmailLogic(string email)
        {
            email = email.CleanParameter<string>();
            if (email.Length <= 50)
            {
                List<string> response = new UserData().AutocompleteByEmail(email);
                return response;
            }
            else
            {
                return null;
            }
        }

        public List<string> AutocompleteByRutLogic(string rut)
        {
            rut = rut.CleanParameter<string>();

            if (!string.IsNullOrWhiteSpace(rut) && rut.Length <= 9)
            {
                List<string> response = new UserData().AutocompleteByRut(rut);
                if (response != null)
                {
                    return response;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public Users GetUserByIdLogic(string id, string key)
        {
            id = Encryption.DecryptData(id, key);
            id = id.CleanParameter<string>();

            if (id.IsNumber())
            {
                Users user = new UserData().GetUserById(Convert.ToInt32(id));

                if (user != null)
                {
                    foreach (Rol r in user.usersRoles)
                    {
                        r.id = Encryption.EncriptData(r.id, key);
                    }

                    return user;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public bool DeleteUserLogic(string id, string key)
        {
            bool response = false;
            id = Encryption.DecryptData(id, key);
            id = id.CleanParameter<string>();

            if (id.IsNumber())
            {
                response = new UserData().DeleteUser(Convert.ToInt32(id));
            }
            return response;
        }

        public bool ChangePasswordLogic(Users i, string userMail, string passwordMail, string mailServer)
        {
            string contrasena = Password.Generate(i);
            i.password = contrasena;
            if (new UserData().ChangePassword(i))
            {
                Thread threadEmail = new Thread(delegate() { Mail.SendMail(i.email, StringBuilderFunctions.JoinString(i.name, " ", i.lastName, " ", "Su nueva password es: ", contrasena), "Cambio Password", userMail, passwordMail, mailServer, null); });
                threadEmail.IsBackground = true;
                threadEmail.Start();
                return true;
            }
            else
            {
                return false;
            }

        }

        public void SendEmailLogic(Users i, string message, string userMail, string passwordMail, string mailServer)
        {
            message = message.CleanParameter<string>();
            Thread threadEmail = new Thread(delegate() { Mail.SendMail(i.email, StringBuilderFunctions.JoinString("Sr. o Sra.", " ", i.GetFullName(), message), "Mensaje Administrador", userMail, passwordMail, mailServer, null); });
            threadEmail.IsBackground = true;
            threadEmail.Start();
        }

        public List<Rol> GetListRolesLogic(string key)
        {
            List<Rol> roles = new UserData().GetListRoles();

            if (roles != null)
            {
                foreach (Rol r in roles)
                {
                    r.id = Encryption.EncriptData(r.id, key);
                }

                foreach (Rol r in roles.Where(x => x.description.ToLower() == "administrador").ToList())
                {
                    roles.Remove(r);
                }

                return roles;
            }
            else
            {
                return null;
            }
        }

        public string EncriptDataLogic(string data, string key)
        {
            return Encryption.EncriptData(data, key);
        }

        public string DecryptDataLogic(string data, string key)
        {
            return Encryption.DecryptData(data, key);
        }
    }
}
